package com.mars.upms.web.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mars.base.common.base.BaseController;
import com.mars.base.common.base.BaseResult;
import com.mars.base.common.exception.AppException;
import com.mars.base.common.utils.Paginator;
import com.mars.upms.entity.UpmsDepartment;
import com.mars.upms.rpc.api.UpmsDepartmentService;

@RestController
@RequestMapping(value = "/api/upms/department")
public class UpmsDepartmentController extends BaseController {

	@Autowired
	private UpmsDepartmentService upmsDepartmentService;

	@PostMapping(value = "/add")
	public Object add(@RequestBody UpmsDepartment entity) {
		try {
			if (StringUtils.isBlank(entity.getName())) {
				return BaseResult.failure("名称不能为空");
			}
			this.upmsDepartmentService.insertSelective(entity);

			return BaseResult.ok();
		} catch (AppException e) {
			throw e;
		}
	}

	@GetMapping(value = "/delete/{id}")
	public Object delete(@PathVariable Integer id) {
		try {
			this.upmsDepartmentService.delete(id);
			return BaseResult.ok();
		} catch (AppException e) {
			throw e;
		}
	}

	@PostMapping(value = "/update")
	public Object update(@RequestBody UpmsDepartment entity) {
		try {
			if (StringUtils.isBlank(entity.getName())) {
				return BaseResult.failure("名称不能为空");
			}
			this.upmsDepartmentService.update(entity);

			return BaseResult.ok();
		} catch (AppException e) {
			throw e;
		}
	}

	@GetMapping(value = "/get/{id}")
	public Object list(@PathVariable Integer id) {
		return this.upmsDepartmentService.selectById(id);
	}
	
	@GetMapping(value = "/list")
	public Object list(UpmsDepartment entity) {
		return this.upmsDepartmentService.selectPage(new Paginator(this.request), entity);
	}

	@GetMapping(value = "/tree")
	public Object tree(UpmsDepartment entity) {
		return this.upmsDepartmentService.tree(entity);
	}
	
	/**
	 * 移除该部门下的所有用户
	 * @param departmentId
	 * @return
	 */
	@GetMapping(value = "/remove_department_all_user")
	public Object removeDepartmentAssignAllUser(Integer departmentId) {
		try {
			this.upmsDepartmentService.removeDepartmentAssignAllUser(departmentId);
			return BaseResult.ok();
		} catch (AppException e) {
			throw e;
		}
	}
	
	/**
	 * 将用户从部门中移除
	 * @param primaryKey
	 * @return
	 */
	@GetMapping(value = "/remove_department_user")
	public Object removeDepartmentAssignUser(String ids) {
		try {
			this.upmsDepartmentService.removeDepartmentAssignUser(ids);
			return BaseResult.ok();
		} catch (AppException e) {
			throw e;
		}
	}
	
}
